//
//  regAndForgPaswdView.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/16.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "regAndForgPaswdView.h"

@implementation regAndForgPaswdView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib {
    [super awakeFromNib];
}
- (void)setType:(NSString *)type {
    _type = type;
    if ([self.type isEqualToString:@"2"]) {
        self.password_tf.placeholder = @"请设置新登录密码";
        self.agenPassword_tf.placeholder = @"请重复新登录密码";
        [self.registerOrForgetPassw_tf setTitle:@"修改密码" forState:0];
    }
}
- (IBAction)codeClick_btn:(UIButton *)sender {
    
    if ([sender.titleLabel.text isEqualToString:@"获取验证码"] || [sender.titleLabel.text isEqualToString:@"重新发送"]) {
        [self code];
    }
    __block NSInteger time = 45; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
   
    dispatch_source_set_event_handler(_timer, ^{
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮的样式
                [self.code_btn setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.code_btn setTitleColor:[UIColor custom_color:@"#333333"] forState:UIControlStateNormal];
                self.code_btn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.code_btn setTitle:[NSString stringWithFormat:@"重新发送(%.2d)", seconds] forState:UIControlStateNormal];
                [self.code_btn setTitleColor:[UIColor custom_color:@"#A91E23"] forState:UIControlStateNormal];
                self.code_btn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    
    dispatch_resume(_timer);
}
- (IBAction)registerOrChangePassword:(id)sender {
    if ([self.type isEqualToString:@"1"]) {//注册
        [self registeredRequest];
    }else {
        [self forgetPassword];
    }
}
- (IBAction)returnClick_btn:(id)sender {
    [self returnLastView];
}

#pragma mark --- 获取验证码
- (void)code {
    if ([self.phoneNumber_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入手机号码"];
        return;
    }
    NSString *url;
    NSDictionary *dic;
    if ([self.type isEqualToString:@"1"]) {//注册
        url = [[apiManger shareApiManger] registersCode];
        dic = @{
                @"phone":self.phoneNumber_tf.text
                };
    }else {//重置
        url = [[apiManger shareApiManger] GetVerificatioVode];
        dic = @{
                @"phoneReg":self.phoneNumber_tf.text
                };
    }
    [networkManger PostHttpDataWithUrlStr:url Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark --- 注册请求
- (void)registeredRequest {
    if ([self.codeVerification_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入验证码"];
        return;
    }else if ([self.password_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入密码"];
        return;
    }
    NSString *result = [self.password_tf.text transactionPasswordReguler];
    if (![result isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:result];
        return;
    }else if(![self.password_tf.text isEqualToString:self.agenPassword_tf.text]) {
        [MBProgressHUD gk_toastShowMessage:@"两次输入的密码不一致"];
        return;
    }
    NSDictionary *dic = @{
                          @"phone":self.phoneNumber_tf.text,
                          @"codeReg":self.codeVerification_tf.text,
                          @"password":self.password_tf.text,
                          @"appType":@"0"
                          };
    __weak typeof(self)weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] registered] Dic:dic SuccessBlock:^(id responseObject) {
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf returnLastView];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark ---
- (void)forgetPassword {
    if ([self.codeVerification_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入验证码"];
        return;
    }else if ([self.password_tf.text isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:@"请输入密码"];
        return;
    }
    NSString *result = [self.password_tf.text transactionPasswordReguler];
    if (![result isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:result];
        return;
    }else if(![self.password_tf.text isEqualToString:self.agenPassword_tf.text]) {
        [MBProgressHUD gk_toastShowMessage:@"两次输入的密码不一致"];
        return;
    }
    NSDictionary *dic = @{
                          @"codeReg":self.codeVerification_tf.text,
                          @"phone":self.phoneNumber_tf.text,
                          @"newPassword":self.password_tf.text
                          };
    __weak typeof(self)weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] resetAndForgetLogInPassword] Dic:dic SuccessBlock:^(id responseObject) {
        
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf returnLastView];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
- (void)returnLastView {
    [UIView transitionWithView:self duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:^{
                    }completion:^(BOOL finish){
                        [self removeFromSuperview];
                    }];
}
@end
